package cat.joronya.supermanager.privateleague;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import cat.joronya.supermanager.R;
import cat.joronya.supermanager.drawer.BaseActionBarActivity;

public class PrivateLeagueDetailActivity extends BaseActionBarActivity
{
	public static final String TAG = "Supervermut";
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	// set layout and call super onCreate
    	setmLayoutResource(R.layout.fragment_layout);
    	setmLayoutId(R.id.fragment_layout);
    	super.onCreate(savedInstanceState);
    	
    	// recuperem id i position del detall passat
    	long id = getIntent().getLongExtra("cat.joronya.supermanager.privateleague.id",0);
		int position = getIntent().getIntExtra("cat.joronya.supermanager.privateleague.position", 0);
		
		// si canviem a landscape i tenim dual pane, llavors llançar PrivateLeagueActivity
    	if( isTablet() && getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE )
    	{
    		// private league list activity
			Intent privateLeagueListActivity = new Intent(getBaseContext(), PrivateLeagueListActivity.class);
			privateLeagueListActivity.putExtra("cat.joronya.supermanager.privateleague.id", id);
			privateLeagueListActivity.putExtra("cat.joronya.supermanager.privateleague.position", position);
    		startActivity(privateLeagueListActivity);
    		overridePendingTransition(0,0);
    		finish();
    	}
    	
    	// setejem el fragment q toca
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.fragment, PrivateLeagueDetailFragment.newInstance(id))
                       .commit();
        
        // titol de la pantalla home inicalment
        setTitle(getResources().getString(R.string.private_league_list_tab));
    } 
    
    @Override
    protected void onNewIntent(Intent intent) 
    {
    	super.onNewIntent(intent);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
		// Handle item selection
        switch (item.getItemId()) 
        {
        	// Respond to the action bar's Up/Home button
        	case android.R.id.home:
        		Intent upIntent = NavUtils.getParentActivityIntent(this);
                NavUtils.navigateUpTo(this, upIntent);
        		return true;
        	default:
        		return super.onOptionsItemSelected(item);
        }
    }
}
